<?php

define("TOKEN", "yhg_weixin_token");

class wechatControl extends BaseHomeControl{


	public function indexOp(){
		require_wechat();
		if(isset($_GET["echostr"])){
			$this->check();
			// echo $_GET['echoStr'];
		}
		else{
			// $wechat = new WeChat($_);
			$this->response();
		}
	}

	private function check(){
		$wechatObj = new wechatCallbackapiTest();
		$wechatObj->valid();


	}


	private function response(){
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		if (empty($postStr)){
			echo '';
			exit;
		}
		libxml_disable_entity_loader(true);
      	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
      	//openid
      	$fromUsername = $postObj->FromUserName = trim($postObj->FromUserName);
      	//wechat_name
        $postObj->ToUserName = trim($postObj->ToUserName);
        //key_word
        $keyword = trim($postObj->Content);

        WeChat::response($postObj);
	}

	public function get_signOp(){
		require_wechat();
		$noncestr = rand(100000,999999);
		$wechat = new WeChat(-1);
		$jsapi_ticket = $wechat->get_jsapi_ticket();
		$timestamp = time();
		$url = $_SERVER['HTTP_REFERER'];
		$string1 = "jsapi_ticket=$jsapi_ticket&noncestr=$noncestr&timestamp=$time&url=$url";
		$sign = sha1($string1);

		exit(json_encode(array(
			'appid' => $wechat->config['appid'],
			'timestamp' => $timestamp,
			'nonceStr' => $noncestr,
			'signature' => $sign,
			'jsApiList' => array('chooseImage')
		)));
		// sha1
	}
}

?>